import { createRouter, createWebHistory } from "vue-router";
import homeView from "@/views/home/home.vue";
import indexView from "@/views/home/index.vue";
import claView from "@/views/home/cla.vue";
import cartView from "@/views/home/cart.vue";
import myView from "@/views/home/my.vue";
import claListView from "@/views/claList/index.vue";
import detailView from "@/views/detail/index.vue";
import dingdanView from "@/views/dingdan/index.vue";
import areaView from "@/views/area/index.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/home/cla",
    },
    {
      path: "/home",
      name: "home",
      component: homeView,
      children: [
        {
          path: "index",
          name: "index",
          component: indexView,
        },
        {
          path: "cla",
          name: "cla",
          component: claView,
        },
        {
          path: "cart",
          name: "cart",
          component: cartView,
        },
        {
          path: "my",
          name: "my",
          component: myView,
        },
      ],
    },
    {
      path: "/claList/:cat_name",
      name: "claList",
      component: claListView,
    },
    {
      path: "/detail/:goods_id",
      name: "detail",
      component: detailView,
    },
    {
      path: "/dingdan",
      name: "dingdan",
      component: dingdanView,
    },
    {
      path: "/area",
      name: "area",
      component: areaView,
    },
  ],
});
router.beforeEach((to, form, next) => {
  if (to.fullPath == "/home/cart" || to.fullPath == "/home/my") {
    let token = localStorage.getItem("token");
    if (token) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});
export default router;
